import axios from "axios";
import {store} from "../store";

const baseURL: string = "/api";

const request = axios.create({
    baseURL:baseURL,
    timeout:5000,
})

request.interceptors.request.use(
    function (config:any) {
      // console.log("每次会执行");
      // config.method
      // config.url
      // config.params
      // config.data
      // 在发送请求之前进行操作
      let token = store.getState().userReducer.user.token;
      console.log(token , 'token');
      if (token) {
        if (config.method.toLocaleLowerCase() == "get") {
          config.params = { ...config.params, accesstoken: token };
        } else {
          config.data = { ...config.data, accesstoken: token };
        }
      }
      return config;
    },
    function (error) {
      // 对请求错误进行操作
      return Promise.reject(error);
    }
  );

export default request;